package Offer.Stack;

import java.util.Stack;

public class GStack {
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] pushed = {1,2,3,4,5};
        int[] popped = {4,5,3,2,1};
        s.validateStackSequences(pushed, popped);
    }
}


// 剑指 Offer 31. 栈的压入、弹出序列
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int pop = 0;
        for(int n : pushed){
            stack.push(n);
            //通过不断进行出站试验得到是不是能全部出站
            while(!stack.isEmpty()&&stack.peek()==popped[pop]){
                stack.pop();
                pop++;
            }
        }
        return stack.isEmpty();
    }
}